package org.jrpc.client2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
import io.netty.channel.SimpleChannelInboundHandler;
import org.jrpc.base.IClient;
import org.jrpc.base.ResultResponse;
import org.jrpc.common.baen.RpcRequest;
import org.jrpc.common.baen.RpcResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/8/29.
 */
@Deprecated
public class RpcClientHandler extends SimpleChannelInboundHandler<RpcResponse> {
    private static final Logger logger = LoggerFactory.getLogger(RpcClientHandler.class);

    private ConcurrentHashMap<String,ResultResponse> map;

    private ChannelHandlerContext ctx;

    private IClient iClient;

    public RpcClientHandler(ConcurrentHashMap<String,ResultResponse> map){
        this.map=map;
    }


    public ResultResponse sendMsg(RpcRequest msg) throws IOException {
//        System.out.println("client:" + msg);
//        System.out.println(msg);
//        RpcResponse response=new RpcResponse();
        ResultResponse resultResponse=new ResultResponse();
        map.put(msg.getRequestId(),resultResponse);
        ctx.channel().writeAndFlush(msg);
        return resultResponse;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        final EventLoop eventLoop = ctx.channel().eventLoop();
        eventLoop.schedule((Runnable) () -> {
            try {
                iClient.init(eventLoop);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 1L, TimeUnit.SECONDS);
        super.channelInactive(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
//        super.channelActive(ctx);
        this.ctx = ctx;
    }

    long startTime = 0;
    long endTime =0;
    int num=1;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse rpcResponse) throws Exception {
//        logger.info("id:"+rpcResponse.getRequestId());
        if(num==1){
            startTime = System.currentTimeMillis();
        }

        ResultResponse res=map.get(rpcResponse.getRequestId());
        res.setRpcResponse(rpcResponse);
//        RpcResponse rpcResponse1=res.getRpcResponse();

//        logger.info("ids:"+rpcResponse1.getRequestId());

        if(num==9999){
            endTime =System.currentTimeMillis();
            logger.info("runTime:"+(endTime-startTime));
        }
        num++;
    }
/*

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if(msg instanceof RpcResponse){
            if(num==1){
                startTime = System.currentTimeMillis();
            }
            RpcResponse res= (RpcResponse) msg;
            ResultResponse resr=map.get(res.getRequestId());
            resr.setRpcResponse(res);

            RpcResponse rpcResponse1=resr.getRpcResponse();
            logger.info("id:"+rpcResponse1.getRequestId());

            if(num==9999){
                endTime =System.currentTimeMillis();
                logger.info("runTime:"+(endTime-startTime));
            }
            num++;
        }
    }*/
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error("api caught exception", cause);
//        ctx.channel().close();
//        ctx.close();
        //重连接

    }

    @Override

    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception{
        //这里执行客户端断开连接后的操作
//        ctx.channel().close();
//        ctx.close();
        logger.info("=======服务端断开=======");
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }
}
